import {Http} from '@angular/http';
import {NgLayer, NgLayerRef} from 'ng2-layer';
import {Component,ViewContainerRef, Injectable}  from '@angular/core';
import {FormsModule}  from '@angular/forms';
import { Notify } from "app/Service";
import {DirectivesM} from "app/Directives";
import {CommonModule} from "@angular/common";

/**
 * 左侧菜单部分
 */
@Component({
	selector: 'aside',
	templateUrl: 'template/app/menu.html'
})
export class MenuComp {
	boxs:any = [];
	box:any;
	
	constructor(
	private http:Http,
	private layer:NgLayer,
	private parent:ViewContainerRef,
	private notify:Notify){
		this.http.get({
			search:{
				m:"Admin",
				c:"ApiBox",
				a:"getBoxBySchool"
			}
		}).then((data)=>{
			this.boxs=data.data.box_list;
		});
		
		/**
		 * 订阅登录成功事件
		 */
		notify.subscribe((isLogin)=>{
			if(!this.boxs){
				http.get({
					search:{
						m:"Admin",
						c:"ApiBox",
						a:"getBoxBySchool"
					}
				}).then((data)=>{
					this.boxs=data.data.box_list;
				});
			}
		});
	}
	
	
	//添加盒子
	addBox(){
		this.layer.dialog({
			dialogComponent:AddBoxComp,
			parent:this.parent,
			title:"添加盒子",
			imports:[FormsModule, DirectivesM, CommonModule ],
			data:{boxs:this.boxs}
		});
	}
}

//添加盒子组件
@Component({templateUrl:"template/dialog/addBox.html"})
class AddBoxComp{
	constructor(private http:Http, private dialog:NgLayerRef){}
	
	box:any = {
		color:"FF5959",
		bg_img:""
	};
	boxs:any;
	
	colors = ["FF5959","F2CA00","00E699","5533FF","FF7919","44e645","4DB1FE","000000"];
	icons = ["&#xe600;","&#xe601;","&#xe602;","&#xe603;","&#xe604;","&#xe605;","&#xe606;","&#xe607;","&#xe608;",
			"&#xe609;","&#xe60a;","&#xe60b;","&#xe60c;","&#xe60d;","&#xe60e;","&#xe60f;","&#xe610;","&#xe611;",
			"&#xe612;","&#xe613;"];
	
	//添加盒子
	addBox(){
		this.http.post({
			message:"保存盒子数据...",
			search:{
				m:"Admin",
				c:"ApiBox",
				a:"addBox"
			},
			body:{
				color:this.box.color,
				bg_img:this.box.bg_img,
				name:this.box.name,
				type:this.box.type
			}
		}).then((data)=>{
			this.dialog.close();
			this.boxs.push(data.data);
		})
	}
}
